/**
 * 
 */
function previewImage(file) {
	var MAXWIDTH = 150;
	var MAXHEIGHT = 200;
	var div = document.getElementById('preview');
	if (file.files && file.files[0]) {
		div.innerHTML = '<img id=imghead >';
		var img = document.getElementById('imghead');
		img.onload = function() {
			var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth,
					img.offsetHeight);
			img.width = rect.width;
			img.height = rect.height;
			img.style.marginLeft = rect.left + 'px';
			img.style.marginTop = rect.top + 'px';
		}
		var reader = new FileReader();
		reader.onload = function(evt) {
			img.src = evt.target.result;
		}
		reader.readAsDataURL(file.files[0]);
	} else {
		var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
		file.select();
		var src = document.selection.createRange().text;
		div.innerHTML = '<img id=imghead>';
		var img = document.getElementById('imghead');
		img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
		var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth,
				img.offsetHeight);
		status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
		div.innerHTML = "<div id=divhead style='width:" + rect.width
				+ "px;height:" + rect.height + "px;margin-top:" + rect.top
				+ "px;margin-left:" + rect.left + "px;" + sFilter + src
				+ "\"'></div>";
	}
}
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
	var param = {
		top : 0,
		left : 0,
		width : width,
		height : height
	};
	if (width > maxWidth || height > maxHeight) {
		rateWidth = width / maxWidth;
		rateHeight = height / maxHeight;

		if (rateWidth > rateHeight) {
			param.width = maxWidth;
			param.height = Math.round(height / rateWidth);
		} else {
			param.width = Math.round(width / rateHeight);
			param.height = maxHeight;
		}
	}

	param.left = Math.round((maxWidth - param.width) / 2);
	param.top = Math.round((maxHeight - param.height) / 2);
	return param;
}
$(document).ready(
			
		function() {
			$('input[type=file]').bootstrapFileInput();
			$('.file-inputs').bootstrapFileInput();
				
			$.ajax({
				url:'../getSessionPara.action',
				type:'post',
				dataType:'json',
				data:{
					propertyName:'isLogin'
				},
				success:function(data)
				{
//					//alert(data);
					if(data=="true")
						{
//						//alert("b");
						if (sessionStorage.getItem("state") != "0") {
							//alert("已报名无法再次报名");
							location.href = "ExamProcess.html";
						} else {
							$.ajax({
								url : '../getGeneral.action',
								type : 'post',
								dataType : 'json',
								data : {},
								success : function(data) {
									var result = eval("(" + data + ")");
									if (result.flag == "true") {
										var array = eval("(" + result.array + ")");
										var a = "";
										$.each(array, function(i, value) {
											a += "<option value = " + value.id + ">"
													+ value.name + "</option>";
										});
										$("#userGeneralType").html(a);
										var selected = $("#userGeneralType").val();
										$.ajax({
											url : '../getSub.action',
											type : 'post',
											dataType : 'json',
											data : {
												gid : selected
											},
											success : function(data1) {
												var result1 = eval("(" + data1 + ")");
												if (result1.flag == "true") {
													var array1 = eval("(" + result1.array
															+ ")");
													var html = "";
													$.each(array1, function(i, value) {
														html += "<option value = "
																+ value.sid + ">"
																+ value.name + "</option>";
													});
													$("#userSubType").html(html);
												}

											}

										});
									}
								}
							});
							$.ajax({
								url : '../queryUser.action',
								type : 'post',
								dataType : 'json',
								data : {
									userId : sessionStorage.getItem("userId")
								},
								success : function(data) {
									var result = eval("(" + data + ")");
									if (result.flag == "true") {
										var userInfo = eval("(" + result.userInfo + ")");
										//alert(userInfo.id);
										$("#name").val(userInfo.name);
										$("#Id").val(userInfo.id);
										$("#EEid").val(userInfo.eeid);
										$("#email").val(userInfo.email);
									}
								}
							})
						}
						
						
						}
					else
						{
//						//alert("a");
						location.href="ExamLogin.htm";
						}
				}
			});
			
			$("#userGeneralType").change(
					function() {
						var selected = $("#userGeneralType").val();
						$
								.ajax({
									url : '../getSub.action',
									type : 'post',
									dataType : 'json',
									data : {
										gid : selected
									},
									success : function(data1) {
										var result1 = eval("(" + data1 + ")");
										if (result1.flag == "true") {
											var array1 = eval("("
													+ result1.array + ")");
											var html = "";
											$.each(array1, function(i, value) {
												html += "<option value = "
														+ value.sid + ">"
														+ value.name
														+ "</option>";
											});
											$("#userSubType").html(html);
										}

									}

								});
					});
			$("#submit").click(function() {
				var info = {
					userId : sessionStorage.getItem("userId"),
					gender : $("#inputGender").val(),
					birthday : $("#birthday").val(),
					nation : $("#nation").val(),
					policy : $("#policy").val(),
					//			 id:$("#Id").val(),
					//			 email:$("#email").val(),
					phoneNo : $("#phoneNo").val(),
					//			 eeid:$("#EEid").val(),
					userDescribe : $("#userDescribe").val(),
					userGeneralType : $("#userGeneralType").val(),
					userSubType : $("#userSubType").val()
				};
				//alert($.toJSON(info));
				$.ajax({
					url : '../sign.action',
					type : 'post',
					dataType : 'json',
					data : {
						userInfo : $.toJSON(info)
					},
					success : function(data) {
						var result = eval("(" + data + ")");
						if (result.flag == "true") {
							$.ajaxFileUpload({
								url : '../fileUpload.action',//用于文件上传的服务器端请求地址  
								secureuri : false,//一般设置为false  
								fileElementId : 'file',//文件上传空间的id属性  <input type="file" id="file" name="file" />  
								dataType : 'json',//返回值类型 一般设置为json  
								data : {
									userName : sessionStorage.getItem("userId")
								},
								success : function(data, status) {
									//						        	//alert(data.result);
									var result = eval("(" + data.result + ")");
									if(result.flag=="true")
										{
										sessionStorage.setItem("state","1");
										location.href="ExamProcess.html";
										}
								},
								error : function(data, status, e) {
									//alert(data.msg);
									//alert(e);
								}
							})
						}
						//alert(result);
					}

				});
			})

		})